package mest.controller.MoneyFlow;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import mest.entity.AccountFile;
import mest.entity.PlanSaleoutMoney;
import mest.entity.User;
import mest.service.MoneyFlow.PlanSaleoutMoneyService;
import mest.service.system.UserService;
import mest.util.NumberConst;
import mest.util.ResultObj;
import mest.util.mestUtil;

/**
 *@Title PlanSaleoutMoneyController.java
 *@description 销售收款计划单
 *@time 2019年12月18日 下午12:06:10
 *@author Twinson Wong
 **/
@RestController
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping(value = "/planSaleoutMoney")
public class PlanSaleoutMoneyController {
	
	@Resource
	PlanSaleoutMoneyService service;
	
	/**
	 * 查询列表
	 * @param jsonObject
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/selectAll")
	public ResultObj selectAll(int index,int pageSize,int status, HttpServletRequest request){
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("index", (index - 1) * pageSize);
			map.put("pageSize", pageSize);
			map.put("status", ""+status);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			//System.out.println(map.toString());
			List<PlanSaleoutMoney> list = service.selectAll(map);
			int count = service.count(map);
			ResultObj resultObj = ResultObj.createSuccess();
			resultObj.setCount(count);
			resultObj.setData(list);
			resultObj.setMsg(resultObj.SELECT_SUCCESS);
			return resultObj;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
	
	/**
	 * 插入 
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/insert")
	public ResultObj insert(@RequestBody JSONObject json, HttpServletRequest request){
		try {
			Map<String, Object> map = json.toJavaObject(Map.class);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			service.insert(map);
			return ResultObj.createSuccessMsg(ResultObj.INSERT_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
	
	/**
	 * 修改
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@PostMapping(value="/update")
	public ResultObj update(@RequestBody JSONObject json, HttpServletRequest request){
		try {
			Map<String, Object> map = json.toJavaObject(Map.class);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			service.update(map);
			return ResultObj.createSuccessMsg(ResultObj.UPDATE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
	
	/**
	 * 删除
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete")
	public ResultObj delete(@RequestBody JSONObject json, HttpServletRequest request){
		try {
			Map<String, Object> map = json.toJavaObject(Map.class);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			service.delete(map);
			return ResultObj.createSuccessMsg(ResultObj.DELETE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}

	/**
	 * 状态
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/changeStatus")
	public ResultObj changeStatus(@RequestBody JSONObject json, HttpServletRequest request){
		try {
			Map<String, Object> map = json.toJavaObject(Map.class);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			service.changeStatus(map);
			return ResultObj.createSuccessMsg(ResultObj.UPDATE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
	
	
}
